package com.exmaple.baidumv.comm;

import java.io.File;

import android.content.Context;

import com.yuelan.codelib.utils.NetWorkUtil;
import com.yuelan.codelib.utils.TextUtil;

public class CacheHelper {
	private static final long CONFIG_CACHE_MOBILE_TIMEOUT = 1000 * 60 * 10;// 默认10分钟
	private static final long CONFIG_CACHE_WIFI_TIMEOUT = 1000 * 60 * 5;// 默认5分钟

	/**
	 * @param cacheFile
	 * @param c
	 * @return 1是读本地。2是去网络抓去。-1是网络异常
	 */
	public static int caCheAvailability(File cacheFile, Context c,
			long wifiTime, long wapTime) {
		String netState = NetWorkUtil.getNetWork(c);
		if (cacheFile.exists() && cacheFile.isFile()) {
			long expiredTime = System.currentTimeMillis()
					- cacheFile.lastModified();
			if (TextUtil.notNull(netState)) {
				if (netState.equals("WIFI")) {
					if (wifiTime == 0) {
						wifiTime = CONFIG_CACHE_WIFI_TIMEOUT;
					}
					if (expiredTime > wifiTime) {
						return 2;
					} else {
						return 1;
					}
				} else {
					if (wapTime == 0) {
						wapTime = CONFIG_CACHE_MOBILE_TIMEOUT;
					}
					if (expiredTime > wapTime) {
						return 2;
					} else {
						return 1;
					}
				}
			} else {
				return 1;
			}
		} else {
			if (TextUtil.notNull(netState)) {
				return 2;
			} else {
				return -1;
			}
		}
	}
}
